function fsb_export(idat,sandbox)

% FSB - MPI: Export data to monline format
%
% EXAMPLE:
% [idat sandbox] = fsb_export_monline('L:\projects\oxana\rat.4F1\TC_SLICE_REALIGNED',65)
%
% INPUT:
% idat : 3D/4D image data
% sandbox: sandbox experiment struct containing the TC struct
%
% OUTPUT:
% matlab data in analyze or monline format
%
% CALLED BY:
% FSB.m
%
% NOTES:
% In development
%
% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer
% License:GNU GPL, no express or implied warranties
%
% $Revision 1.0
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

export_GUI = figure('Visible','off','Position',[800,200,150,340],'Resize','off','Toolbar','none','MenuBar','none','NumberTitle','off','Name','FSB Export GUI');

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Add Buttons and edit fields
%~~~~~~~~~~~~~~~~~~~~~~~~~~

uicontrol('Style','Pushbutton',...
    'Position',[20,300,60,25],...
    'String','Export',...
    'TooltipString','Export',...
    'Callback',{@run_pipeline_Callback});

h_meanselect = uicontrol('Style','radiobutton','String','Write Mean images',...
    'Position',[20,80,120,25],'Visible','on',...
    'TooltipString','Write Mean images',...
    'Callback',{@meanselect_Callback});

h_prefixt = uicontrol('Style','text',...
    'Position',[20,270,50,20],...
    'String','Prefix','Visible','on');

h_prefix = uicontrol('Style','edit',...
    'Position',[80,270,50,20],...
    'String','','Visible','on',...
    'TooltipString','Prefix for file',...
    'Callback',{@prefix_Callback});

h_4dsel = uicontrol('Style','radiobutton','String','Write 3D',...
    'Position',[20,50,120,25],'Visible','on',...
    'TooltipString','Write 3D Nifti Images',...
    'Callback',{@sel3D_Callback});

h_niisel = uicontrol('Style','radiobutton','String','Write .nii',...
    'Position',[20,20,120,25],'Visible','on',...
    'TooltipString','Write .nii Nifti Images',...
    'Callback',{@selnii_Callback});


%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Create a button group for data to work on
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
h1 = uibuttongroup('visible','off','Units','pixels','Position',[20 110 90 150],'Title','Target');

x0 = uicontrol('Style','Radio','String','Monline',...
    'pos',[10 100 70 30],'parent',h1,'HandleVisibility','off');

x1 = uicontrol('Style','Radio','String','Analyze',...
    'pos',[10 70 70 30],'parent',h1,'HandleVisibility','off');
x2 = uicontrol('Style','Radio','String','Nifti',...
    'pos',[10 40 70 30],'parent',h1,'HandleVisibility','off');
x3 = uicontrol('Style','Radio','String','2Dseq',...
    'pos',[10 10 70 30],'parent',h1,'HandleVisibility','off');

set(h1,'SelectionChangeFcn',@Get_Export_Method_Callback);
set(h1,'SelectedObject',x2);  % Select Nifti
set(h1,'Visible','on');

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Wait until user input is done
%~~~~~~~~~~~~~~~~~~~~~~~~~~

expopts.mean = 0;
expopts.prefix = '';
expopts.ver = 3;
expopts.nii = 0;
expopts.threeD = 1;

set(export_GUI,'Visible','on');
axis off;

uiwait;

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Export method callback
%~~~~~~~~~~~~~~~~~~~~~~~~~~

    function Get_Export_Method_Callback(source,eventdata)

        selstr = get(eventdata.NewValue,'String');
        disp (selstr);
       
        switch selstr
            
            case 'Monline' % Yusuke Murayama Monline format
                expopts.ver = 1;
                set(h_meanselect,'Visible','on');
                set(h_prefix,'Visible','on');
                set(h_prefixt,'Visible','on');
                set(h_4dsel,'Visible','off');
                set(h_niisel,'Visible','off');

            case 'Analyze' % Standard Analyze format
                expopts.ver = 2;
                set(h_meanselect,'Visible','on');
                set(h_prefix,'Visible','on');
                set(h_prefixt,'Visible','on');
                set(h_4dsel,'Visible','off');
                set(h_niisel,'Visible','off');

            case 'Nifti' % Nifti format
                expopts.ver = 3;
                set(h_meanselect,'Visible','on');
                set(h_prefix,'Visible','on');
                set(h_prefixt,'Visible','on');
                set(h_4dsel,'Visible','on');
                set(h_niisel,'Visible','on');

            case '2Dseq' % Bruker 2Dseq format
                expopts.ver = 4;
                set(h_meanselect,'Visible','off');
                set(h_prefix,'Visible','off');
                set(h_prefixt,'Visible','off');
                set(h_4dsel,'Visible','off');
                set(h_niisel,'Visible','off');
        end

    end

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Prefix callback
%~~~~~~~~~~~~~~~~~~~~~~~~~~

    function prefix_Callback(source,eventdata)
        prefix_string = get(h_prefix,'String');
        expopts.prefix = prefix_string;
    end

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Select 3D callback
%~~~~~~~~~~~~~~~~~~~~~~~~~~

    function sel3D_Callback(source,eventdata)
        if (get(h_4dsel,'Value') == get(h_4dsel,'Max')) ;
            expopts.threeD = 1;
        else
            expopts.threeD = 0;
        end
    end

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Select nii callback
%~~~~~~~~~~~~~~~~~~~~~~~~~~

    function selnii_Callback(source,eventdata)
        if (get(h_niisel,'Value') == get(h_niisel,'Max')) ;
            expopts.nii = 1;
        else
            expopts.nii = 0;
        end
    end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Write mean images Callback
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function meanselect_Callback(source,eventdata)
        if (get(h_meanselect,'Value') == get(h_meanselect,'Max')) ;
            expopts.mean = 1;
        else
            expopts.mean = 0;
        end

    end

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Run selected options
%~~~~~~~~~~~~~~~~~~~~~~~~~~

    function run_pipeline_Callback(source,eventdata)

        % check if filename prefix provided
        if ~isempty(expopts.prefix)
            name = [expopts.prefix '_'];
        else
            name = 'new_';
        end

        % select export target
        switch expopts.ver
            
            case 1 % run YM monline export
                fsb_export_monline(idat,sandbox,expopts);

            case 2 % run Analyze export

                idat=full(idat);

                if isfield(sandbox,'hdr')&& ~isempty(sandbox.hdr);
                    fsb_img_write(idat,name,sandbox.hdr,4,0);
                    sandbox.name = name;
                else
                    fsb_img_write(idat,name,'template.hdr');
                end

                if expopts.mean ==1 % Write mean images if desired
                    meanname = [name '_mean'];
                    idat_mean = mean(idat,4);
                    fsb_img_write(idat_mean,meanname,sandbox.hdr,4,0);
                end

            case 3 % run Nifti Export

                idat=full(idat);

                % get header dimensions if not provided
                if ~isfield(sandbox,'hdr')&& ~isempty(sandbox.hdr);
                    sandbox.hdr.dime.pixdim(2:4) = [1 1 1];
                end

                % Check if nii or hdr/img should be written
                if expopts.nii ==0;
                    fileext = '.hdr';
                else
                    fileext = '.nii';
                end

                h = waitbar(0,'Writing images...');
                
                if expopts.threeD ==0 % Check if 4D files should be written
                    nii = make_nii(idat, sandbox.hdr.dime.pixdim(2:4), [0 0 0]); %, [datatype], [description])
                    filename2 = [name fileext];
                    save_nii(nii, filename2);
                else
                    for x = 1:size(idat,4); % Write 3D images
                        waitbar(x/size(idat,4));
                        img = idat(:,:,:,x);
                        filename2 = sprintf('%05.0f', x);
                        filename2 = [name filename2 fileext];
                        nii = make_nii(img, sandbox.hdr.dime.pixdim(2:4), [0 0 0]); %, [datatype], [description])
                        save_nii(nii, filename2);

                    end
                    
                end

close(h)
                if expopts.mean ==1 % Write mean image if desired
                    meanname = [name 'mean' fileext];
                    idat_mean = mean(idat,4);
                    nii = make_nii(idat_mean, sandbox.hdr.dime.pixdim(2:4), [0 0 0]);
                    save_nii(nii, meanname);
                end

        end

    end
end